package day20130911;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;

public class sql extends JFrame {

	private JPanel contentPane;
	private JTextField txtSelectFrom;
	private Connection conn;
	public void dbConn(){
		String url="jdbc:oracle:thin:@127.0.0.1:1521";
		String user="hr";
		String password = "hr";
		

		try {
			// 1) 오라클 드라이버 로딩
			Class.forName("oracle.jdbc.driver.OracleDriver");

			// 2) 오라클 DB와 연결 (connection 생성)
			// Connection이 Interface 이므로 new로 할 수 없음
			conn = DriverManager.getConnection(url, user, password);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	public  void find(){
	
		dbConn();
		
		try{
			PreparedStatement pstmt = conn.prepareStatement(txtSelectFrom.getText().trim());
					ResultSet rs= pstmt.executeQuery();
					//테이블의 타이틀를 지정하기위해 MetaData 가져옴
					ResultSetMetaData rsm = rs.getMetaData();
					Vector<String> title = new Vector<>();
					Vector data = null;
					
					DefaultTableModel dtm = new DefaultTableModel();
					
					for(int i = 1; i<rsm.getColimnCount(); i++)// i<rsm.getColimnCount() 의 i는 맨마지막 데이터는 선택이 되지 않는다를 가정
						title.add(rsm.getColumnName(i));
					
				
				dtm.selColumnIdentifiers(title);
				table.setModel(dtm);
		
				while(rs.next()){
					data = new Vector();
					for(int i = 1; i<rsm.getColumnCount(); i++){
						if(rsm.getColumnType(i)== Types.INTEGER){
							data.add(new Integer(rs.getInt(i)));
							
						}else{
							data.add(rs.getString(i));
						}
					}
				}
		
		}catch(Exception ex){
			ex.printStackTrace();
		}
}
		

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					sql frame = new sql();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public sql() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("SQL");
		panel.add(lblNewLabel, BorderLayout.WEST);
		
		txtSelectFrom = new JTextField();
		txtSelectFrom.setText("select * from emp");
		panel.add(txtSelectFrom, BorderLayout.CENTER);
		txtSelectFrom.setColumns(10);
		
		JButton btnNewButton = new JButton("New button");
		panel.add(btnNewButton, BorderLayout.EAST);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, BorderLayout.CENTER);
		
		JList list = new JList();
		scrollPane.setViewportView(list);
	}

}
